﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Core.Controllers
{
	using System.Web.Mvc;
	using Falcon.Core.Domain;
	using Falcon.Core.Domain.Services;
	using Falcon.Core.Domain.Values;
	using Falcon.Core.Extensions;
	using Falcon.Core.Validation;

	[Authorize]
	public class SystemResourceController : DefaultController
	{
		public SystemResourceController(IResourceService resourceService)
		{
			this.ResourceService = resourceService;
		}

		public IResourceService ResourceService
		{
			get;
			private set;
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Index(int? page)
		{
			var resources = ResourceService.GetPaged(new ResourceSpecification
			{
				Page = page,
				Limit = Settings.Resources.PageLimit
			});

			var resource = resources.FirstOrDefault();

			return resource.CanView ? View(Views.Index, Masters.System, resources) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			var resource = ResourceService.Create();

			return resource.CanCreate ? View(Views.Create, Masters.System, new ResourceCreate()) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(ResourceCreate value)
		{
			try
			{
				var resource = ResourceService.Create();

				if (!resource.CanCreate)
				{
					return NotAuthorized();
				}

				this.ResourceService.Insert(resource, value);
				this.ResourceService.Rebuild();

				return Message(Routes.SystemResourceUpdate, new { id = resource.Id }, Resources.Messages.ResourceCreated.Fmt(resource.Key));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Create, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Update(int id)
		{
			var resource = ResourceService.GetById(id);

			if (resource == null)
			{
				return Error(Resources.Messages.ResourceNotFound);
			}

			return resource.CanUpdate ? View(Views.Update, Masters.System, new ResourceUpdate(resource)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Update(ResourceUpdate value)
		{
			try
			{
				var resource = ResourceService.GetById(value.Id);

				if (resource == null)
				{
					return Error(Resources.Messages.ResourceNotFound);
				}

				if (!resource.CanUpdate)
				{
					return NotAuthorized();
				}

				this.ResourceService.Update(resource, value);
				this.ResourceService.Rebuild();

				return Message(Routes.SystemResourceUpdate, new { id = resource.Id }, Resources.Messages.ResourceUpdated.Fmt(resource.Key));
			}
			catch (ValidationException exception)
			{
				ModelState.CopyValidationExceptions(exception);
			}

			return View(Views.Update, Masters.System, value);
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Delete(int id)
		{
			var resource = this.ResourceService.GetById(id);

			if (resource == null)
			{
				return base.Error(Resources.Messages.ResourceNotFound);
			}

			return resource.CanDelete ? base.View(Views.Delete, Masters.System, new ResourceDelete(resource)) : NotAuthorized();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Delete(ResourceDelete value)
		{
			try
			{
				var resource = this.ResourceService.GetById(value.Id);

				if (resource == null)
				{
					return base.Error(Resources.Messages.ResourceNotFound);
				}

				if (!resource.CanDelete)
				{
					return NotAuthorized();
				}

				this.ResourceService.Delete(resource);

				return base.RedirectToRoute(Routes.SystemResourceIndex);
			}
			catch (ValidationException exception)
			{
				base.ModelState.CopyValidationExceptions(exception);
			}

			return base.View(Views.Delete, Masters.System, value);
		}
	}
}